package Siebel::Code::Analyzer::DAO::ODBC;

use warnings;
use strict;
use Moose;
use DBI qw(:sql_types);
use namespace::autoclean;

extends 'Siebel::Code::Analyzer::DAO';

has user     => ( isa => 'Str', is => 'ro', required => 1 );
has password => ( isa => 'Str', is => 'ro', required => 1 );

override '_build__dbh' => sub {

    my $self = shift;

    my $dbh =
      DBI->connect( $self->data_source(), $self->user(), $self->password() );

    $dbh->{RaiseError}  = 1;
    $dbh->{LongReadLen} = $self->max_long();

    return $dbh;

};

override 'get_runtime_query' => sub {

    my $self = shift;

    # this query is for runtime BS
    my $sql = <<BLOCK;
SELECT B.NAME,
  B.DISPLAY_NAME,
  a.INACTIVE_FLG,
  B.CLASS_NAME,
  b.SVR_COMPAT_FLG,
  B.COMMENTS,
  A.NAME   AS METHOD_NAME,
  A.SCRIPT AS METHOD_CODE
FROM SIEBEL.S_RT_SVC_SCRPT A,
  SIEBEL.S_RT_SVC B
WHERE a.RT_SVC_ID = B.ROW_ID
AND A.PROG_LANG   = 'JS'
BLOCK

    return $sql;

};

override 'get_repo_queries' => sub {

    my $self = shift;

my $in_clause = super();

    my @queries;

    my $sql = <<BLOCK;
SELECT
  r.name        AS repository_name,
  'Application' AS obj_type ,
  a.name        AS application_name,
  ast.name      AS script_name,
  ast.last_upd,
  ast.script
FROM
  SIEBEL.S_APPL_SCRIPT ast,
  SIEBEL.S_APPLICATION a,
  SIEBEL.S_REPOSITORY r
WHERE
  r.row_id             = ast.repository_id
AND ast.application_id = a.row_id
AND ast.repository_id  = a.repository_id
AND R.NAME             in ($in_clause) 
AND ast.last_upd       > {fn CONVERT(?, SQL_TIMESTAMP)};
BLOCK

    push( @queries, $sql );

    $sql = <<BLOCK;
SELECT
  r.name   AS repository_name,
  'Applet' AS obj_type ,
  a.name   AS applet_name,
  ast.name AS script_name,
  ast.last_upd,
  ast.script
FROM
  SIEBEL.S_APPL_WEBSCRPT ast,
  SIEBEL.S_APPLET a,
  SIEBEL.S_REPOSITORY r
WHERE
  r.row_id            = ast.repository_id
AND ast.applet_id     = a.row_id
AND ast.repository_id = a.repository_id
AND R.NAME            in ($in_clause) 
AND ast.last_upd      > {fn CONVERT(?, SQL_TIMESTAMP)};
BLOCK

    push( @queries, $sql );

    $sql = <<BLOCK;
SELECT
  r.name               AS repository_name,
  'Business Component' AS obj_type ,
  b.name               AS bc_name,
  bst.name             AS script_name,
  bst.last_upd,
  bst.script
FROM
  SIEBEL.S_BUSCOMP_SCRIPT bst,
  SIEBEL.S_BUSCOMP b,
  SIEBEL.S_REPOSITORY r
WHERE
  r.row_id            = bst.repository_id
AND bst.buscomp_id    = b.row_id
AND bst.repository_id = b.repository_id
AND R.NAME            in ($in_clause) 
AND bst.last_upd      > {fn CONVERT(?, SQL_TIMESTAMP)};
BLOCK

    push( @queries, $sql );

    $sql = <<BLOCK;
SELECT C.NAME        AS REPOSITORY_NAME,
  'Business Service' AS OBJ_TYPE ,
  B.NAME             AS SERVICE_NAME,
  A.NAME             AS METHOD_NAME,
  A.LAST_UPD,
  A.SCRIPT
FROM SIEBEL.S_SERVICE_SCRPT A,
  SIEBEL.S_SERVICE B,
  SIEBEL.S_REPOSITORY C
WHERE C.ROW_ID      = A.REPOSITORY_ID
AND A.SERVICE_ID    = B.ROW_ID
AND A.REPOSITORY_ID = B.REPOSITORY_ID
AND bst.last_upd      > {fn CONVERT(?, SQL_TIMESTAMP)};
AND C.NAME          in ($in_clause)
AND A.LAST_UPD      > {fn CONVERT(?, SQL_TIMESTAMP)};
BLOCK

    push( @queries, $sql );

    return $self->_bind_params(\@queries); 

};

=pod

=head1 COPYRIGHT AND LICENSE

This software is copyright (c) 2012 of Alceu Rodrigues de Freitas Junior, E<lt>arfreitas@cpan.org<E<gt>

This project is based on the code (SiebelOVAnalyzer) provided in the article "Memory Leaks in 
Siebel" (http://siebelunleashed.com/memory-leaks-siebel/) from Timur Vafin. 

This file is part of Siebel Code Analyzer.

Siebel Code Analyzer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

Siebel Code Analyzer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with Siebel Monitoring Tools.  If not, see <http://www.gnu.org/licenses/>.

=cut

__PACKAGE__->meta->make_immutable;
1;
